// 全局请求封装
// export const base_url = 'http://192.168.3.184:8095/api/'
export const base_url = 'http://192.168.3.184:8097/api'
// export const base_url="https://qytc.quyike.cn/api"

// 请求超出时间
const timeout = 5000

// 需要修改token，和根据实际修改请求头
export default (params) => {
	let url = params.url;
	let method = params.method || "get";

	var headData = {}

	console.log("++++++++=" + params.data)
	if (params.data != 'undefined' && params.data != '' && params.data != null) {
		let ids = Object.keys(params.data);
		console.log("----------"+ids)
		ids.forEach(function(value, key) {
			console.log("id:" + value, "number:" + params.data[value]);
			headData[value] = params.data[value]
		})
	}


	var user_id = uni.getStorageSync("user_id")
	
	
	

	if (user_id) {
		headData.user_id = user_id
	}
	
	console.log("++++++++=" + headData)
	let ids = Object.keys(headData);
	console.log("++++++++=" + ids)
	ids.forEach(function(value, key) {
		console.log("key:" + value, "value:" + headData[value]);
		
	})

	let data = headData || {};
	let header = {
		// 'Blade-Auth': uni.getStorageSync('token') || '',
		'Content-Type': 'application/json;charset=UTF-8',
		// 'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
		// 'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx', // avue配置相关
		// ...params.header
	}
	if (method == "post") {
		header = {
			'Content-Type': 'application/json;charset=UTF-8', // 自定义，跟后台约定好传什么格式的
		};
	}
	// uni.showLoading();
	return new Promise((resolve, reject) => {
		console.log("Promise")
		uni.request({
			url: base_url + url,
			method: method,
			header: header,
			data: data,
			timeout,
			success(response) {
				const res = response
				// 根据返回的状态码做出对应的操作
				//获取成功
				console.log(res.statusCode);
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					
					switch (res.statusCode) {
						
						case 404:
							uni.showToast({
								title: '请求地址不存在...',
								duration: 2000,
							})
							break;
						default:
							uni.showToast({
								title: '请重试...',
								duration: 2000,
							})
							break;
					}
				}
			},
			fail(err) {
				console.log(err)
				if (err.errMsg.indexOf('request:fail') !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);

			},
			complete() {
				console.log("------"+444444)
				// 不管成功还是失败都会执行
				uni.hideLoading();
				uni.hideToast();
			}
		});
	}).catch(() => {});
};

export const setUserId = (params) => {

	return params

}